<?php
/* class pagination
author -> devent
date -> 02/11/09
site -> livetrance.net */
class pagination {
    private $d,$q,$page,$totalrows,$pagination;
    public $p;
    
    function __construct($rpp,$sql) {
        $this->rpp = $rpp; $this->sql = $sql;
    }
    
    private function getTotalRows() {
        $this->d = mysql_fetch_assoc(mysql_query($this->sql));
        $this->totalrows = $this->d['allz'];
        return $this->d['allz'];
    }
    
    public function page($page) {
        $this->page = (!isset($page) || $page < 0) ? $this->page = 1 : $this->page = $page;
    }
    
    private function lastPage() {
        return ceil($this->getTotalRows() / $this->rpp);
    }
    
    public function limit() {
        return ($this->page - 1) * $this->rpp;
    }
    
    public function showPages() {
        if($this->lastPage() != "1") {
            $this->pagination = '<div id="pagination">';
            $this->d = ceil($this->getTotalRows() / $this->rpp);
            $prev = ($this->page <= 1) ? '<div class="disabled button">Предишна</div>' : '<div class="enabled button"><a href="javascript:pagination('.($this->page - 1).')">Предишна</a></div>';
            $next = ($this->page >= $this->lastPage()) ? '<div class="disabled button">Следваща</div>' : '<div class="enabled button"><a href="javascript:pagination('.($this->page + 1).')">Следваща</a></div>';
            $this->pagination .= $prev;
            if($this->lastPage() > 7) {
                if($this->page > 3) { $this->pagination .= '<a class="button" href="javascript:pagination(1)">1</a> ...'; }
                if($this->page <= 3) {
                    for($x = 1;$x<=4;$x++) { $this->pagination .= '<a class="button" href="javascript:pagination('.$x.')">'.$x.'</a>'; }
                    $this->pagination .= '<div class="marg">...</div><a class="button" href="javascript:pagination('.$this->lastPage().')">'.$this->lastPage().'</a>';
                } else {
                    if($this->page >= ($this->lastPage() - 4)) {
                        for($x = ($this->lastPage() - 3);$x<=($this->lastPage() - 1);$x++) { $this->pagination .= '<a class="button" href="javascript:pagination('.$x.')">'.$x.'</a>'; }
                        $this->pagination .= '<a class="button" href="javascript:pagination('.$this->lastPage().')">'.$this->lastPage().'</a>';
                    }
                    else { 
                        for($x = $this->page;$x<=($this->page + 4);$x++) { $this->pagination .= '<a class="button" href="javascript:pagination('.($x - 1).')">'.($x - 1).'</a>'; }
                        $this->pagination .= '<div class="marg">...</div><a class="button" href="javascript:pagination('.$this->lastPage().')">'.$this->lastPage().'</a>';
                    }
                }
            } else {
                for($x = 1;$x<=$this->lastPage();$x++) { $this->pagination .= '<a class="button" href="javascript:pagination('.$x.')">'.$x.'</a>'; }
            }
            $this->pagination .= $next.'</div>';
            return $this->pagination;
        }
    }
}
?>
